cmake_minimum_required(VERSION 3.10)
project(
    wvf
    VERSION 0.0.1
    HOMEPAGE_URL "https://gitee.com/jsm920/wvf"
    DESCRIPTION "本项目是仿Vue，设计一套 HTML 编译器，仅生成一个html/js/css。LICENSE:MIT。"
    LANGUAGES C CXX
)
set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/.build)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/bin)
string(TIMESTAMP COMPILE_TIME %Y%m%d%H%M%S)

# 打包源代码到源码根目录的上级目录
add_custom_target(pack_src
    WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/../
    COMMAND tar --exclude=bin --exclude=build --exclude=lib --exclude=cert -Jcvf ${CMAKE_PROJECT_NAME}.${COMPILE_TIME}.tar.xz wvf 
    COMMENT "pack source code to the file ${PROJECT_SOURCE_DIR}/../${CMAKE_PROJECT_NAME}.${COMPILE_TIME}.tar.xz"
)

if("${CMAKE_HOST_SYSTEM_NAME}" STREQUAL "Windows" )
    if("${CMAKE_C_COMPILER_ID}" STREQUAL "MSVC" OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
        set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG ${PROJECT_SOURCE_DIR}/bin)
        set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE ${PROJECT_SOURCE_DIR}/bin)
        # add_compile_options(/utf-8)
        # add_compile_options(/source-charset:utf-8 /execution-charset:gb2312)
    endif()
elseif("${CMAKE_HOST_SYSTEM_NAME}" STREQUAL "Linux")
endif()
add_executable(httpd
    src/httpd/httpd.c
    src/httpd/httpd.h
    # src/httpd/cfg.c
    # src/httpd/cfg.h
    src/httpd/tree.c
    src/httpd/tree.h
    src/httpd/array.c
    src/httpd/array.h
    src/httpd/list.c
    src/httpd/list.h
    src/httpd/mio.c
    src/httpd/mio.h
)
if("${CMAKE_BUILD_TYPE}" STREQUAL "Debug")
    target_compile_definitions(httpd 
        PUBLIC
        -D_DEFAULT_SOURCE=1 #防止vscode 提示 mio.c 中 struct ip_mreq 未定义（ 在 mio.c 中已定义）
    )
endif()
target_include_directories(httpd BEFORE
    PUBLIC
        src/httpd/
)
if("${CMAKE_HOST_SYSTEM_NAME}" STREQUAL "Windows" )
    if("${CMAKE_C_COMPILER_ID}" STREQUAL "MSVC" OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
    endif()
elseif("${CMAKE_HOST_SYSTEM_NAME}" STREQUAL "Linux")
    target_link_libraries(httpd 
        m # for #include <math.h>
    )
endif()

# tools win hide 程序

if("${CMAKE_HOST_SYSTEM_NAME}" STREQUAL "Windows" )
    add_executable(nohup
        src/tools/win/nohup/nohup.c
    )
    target_compile_definitions(nohup
        PUBLIC
            UNICODE _UNICODE
    )
endif()

if("${CMAKE_BUILD_TYPE}" STREQUAL "Debug")
# 测试相关源文件
add_executable(debug
    src/httpd/test/debug.c
    src/httpd/array.c
    src/httpd/array.h
    src/httpd/list.c
    src/httpd/list.h
    src/httpd/tree.c
    src/httpd/tree.h
    src/httpd/cfg.c
    src/httpd/cfg.h
    src/httpd/mio.c
    src/httpd/mio.h
)
target_compile_definitions(debug 
    PUBLIC
    -D_DEFAULT_SOURCE=1 
)
target_include_directories(debug BEFORE
    PUBLIC
        src/httpd/
)
if("${CMAKE_HOST_SYSTEM_NAME}" STREQUAL "Linux")
    target_link_libraries(debug 
        m # for #include <math.h>
    )
endif()
# 对比测试
add_executable(testcpp
    src/httpd/test/test.cpp
)
endif()